# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
互动易
http://irm.cninfo.com.cn/ircs/index
"""

class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": ua.random
            # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15'

        }


    def get_contents(self, cursor):
        postData = {
            'stockcode': '300773',
            'orgId': '9900034778',
            'pageSize': '10',
            'pageNum': cursor #'12'
        }
        url = "http://irm.cninfo.com.cn/ircs/company/dynamic"
        html = HttpUtils.do_request("POST", url, self.headers, postData)

        data_json = json.loads(html.text)
        total = data_json['total']  # 总数据
        totalPage = data_json['totalPage']  # 总页数
        for d in data_json['rows']:
            dict_data = dict()
            dict_data['stockCode'] = d['stockCode']  # 股票代码
            dict_data['companyShortName'] = d['companyShortName']  # 股票名称
            if d['contentType'] == 3:
                dict_data['contentType'] = "公司公告"
            elif d['contentType'] == 1 and d['contentType'] == 11:
                dict_data['contentType'] = "问答"
            dict_data['mainContent'] = d['mainContent']  # 标题（问）
            dict_data['attachedContent'] = d['attachedContent']  # 内容（答）
            dict_data['attachmentUrl'] = d['attachmentUrl']  # 链接
            # 发布时间
            dict_data['updateDate'] = datetime.fromtimestamp(int(d['pubDate']) / 1000).strftime("%Y-%m-%d %H:%M:%S")
            # 回复时间
            dict_data['updateDate'] = datetime.fromtimestamp(int(d['updateDate']) / 1000).strftime("%Y-%m-%d %H:%M:%S")
            dict_data['authorName'] = d['authorName']  # 用户名
            dict_data['author'] = d['author']  # id
            dict_data['praiseCount'] = d['praiseCount']  # 点赞数量
            dict_data['forwardCount'] = d['forwardCount']  # 分享数量
            dict_data['favoriteCount'] = d['favoriteCount']  # 收藏数量


            print(str(dict_data))
        # 循环
        if cursor < totalPage:
            self.get_contents(cursor + 1)


if __name__ == '__main__':
    test_get = Test_Get()
    test_get.get_contents(1)
